package cn.iocoder.yudao.module.train.controller.admin.teacher;

import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.TeacherUserDTO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.PHONE_NUMBER_TOOLONG;

import cn.iocoder.yudao.module.train.controller.admin.teacher.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.teacher.TeacherDO;
import cn.iocoder.yudao.module.train.service.teacher.TeacherService;

@Tag(name = "管理后台 - 教师用户")
@RestController
@RequestMapping("/train/teacher")
@Validated
public class TeacherController {

    @Resource
    private TeacherService teacherService;

    @PostMapping("/create")
    @Operation(summary = "创建教师用户")
    @PreAuthorize("@ss.hasPermission('train:teacher:create')")
    public CommonResult<Long> createTeacher(@Valid @RequestBody TeacherSaveReqVO createReqVO) {
        String phone = createReqVO.getTeacherPhoneNumber();
        if (phone == null || !phone.matches("^1\\d{10}$")) {
            throw exception(PHONE_NUMBER_TOOLONG);
        }
        return success(teacherService.createTeacher(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新教师用户")
    @PreAuthorize("@ss.hasPermission('train:teacher:update')")
    public CommonResult<Boolean> updateTeacher(@Valid @RequestBody TeacherSaveReqVO updateReqVO) {
        String phone = updateReqVO.getTeacherPhoneNumber();
        if (phone == null || !phone.matches("^1\\d{10}$")) {
            throw exception(PHONE_NUMBER_TOOLONG);
        }
        teacherService.updateTeacher(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除教师用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:teacher:delete')")
    public CommonResult<Boolean> deleteTeacher(@RequestParam("id") Long id) {
        teacherService.deleteTeacher(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除教师用户")
                @PreAuthorize("@ss.hasPermission('train:teacher:delete')")
    public CommonResult<Boolean> deleteTeacherList(@RequestParam("ids") List<Long> ids) {
        teacherService.deleteTeacherListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得教师用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:teacher:query')")
    public CommonResult<TeacherRespVO> getTeacher(@RequestParam("id") Long id) {
        TeacherUserDTO teacher = teacherService.getTeacher(id);
        return success(BeanUtils.toBean(teacher, TeacherRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得教师用户分页")
    @PreAuthorize("@ss.hasPermission('train:teacher:query')")
    public CommonResult<PageResult<TeacherRespVO>> getTeacherPage(@Valid TeacherPageReqVO pageReqVO) {
        PageResult<TeacherUserDTO> pageResult = teacherService.getTeacherPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TeacherRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得教师用户分页")
    @PreAuthorize("@ss.hasPermission('train:teacher:query')")
    public CommonResult<List<TeacherRespVO>> getTeacherList(@Valid TeacherPageReqVO pageReqVO) {
        List<TeacherUserDTO> pageResult = teacherService.getTeacherList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TeacherRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出教师用户 Excel")
    @PreAuthorize("@ss.hasPermission('train:teacher:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTeacherExcel(@Valid TeacherPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TeacherUserDTO> list = teacherService.getTeacherPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "教师用户.xls", "数据", TeacherRespVO.class,
                        BeanUtils.toBean(list, TeacherRespVO.class));
    }

}
